package com.pet.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pet.service.SysUserService;
import com.pet.system.SysUser;
import com.pet.utils.MD5Helper;
import com.pet.utils.Result;
import com.pet.vo.SysUserQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "用户管理器")
@RequestMapping("/lovePet/system/sysUser")
@CrossOrigin("*")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;


    @GetMapping("/findAll")
    public Result findAllPet(){
        List<SysUser> list = sysUserService.list();
        return Result.ok(list);
    }


    //分页和条件查询
    @ApiOperation("分页和条件查询")
    @GetMapping("{page}/{limit}")
    public Result selectUserPageByVo(@PathVariable Long page,
                                     @PathVariable Long limit,
                                     SysUserQueryVo sysUserQueryVo){
        IPage<SysUser> iPage=new Page<>(page,limit);
        iPage=this.sysUserService.selectpage(iPage,sysUserQueryVo);
        return Result.ok(iPage);
    }

    @ApiOperation("添加用户")
    @PostMapping("/addUser")
    public Result addUser(@RequestBody SysUser sysUser){
        //通过MD5 工具类
//        String passwordWidthMD5 = MD5Helper.encrypt(sysUser.getPassword());
        //将加密密码设置给sysUser
        sysUser.setPassword(new BCryptPasswordEncoder().encode(sysUser.getPassword()));
        boolean result = this.sysUserService.save(sysUser);
        if (result){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    @ApiOperation("根据id查找用户")
    @GetMapping("/findUserById/{id}")
    public Result getUserById(@PathVariable Long id){
        SysUser sysUser = this.sysUserService.getById(id);
        return Result.ok(sysUser);
    }


    @ApiOperation("更新用户")
    @PostMapping ("/updateUser")
    public Result updateUser(@RequestBody SysUser sysUser){
        boolean result = this.sysUserService.updateById(sysUser);
        if (result){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    // 批量删除
    @ApiOperation("批量删除")
    @DeleteMapping("/removeBatchUserByIds")
    public Result removeBatchUserByIds(@RequestBody List<Long> ids)
    {
        boolean b = this.sysUserService.removeByIds(ids);
        if (b)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }
    //单个删除
    @ApiOperation("删除用户")
    @DeleteMapping("/deleteUsersByIds/{id}")
    public Result deleteUsersByIds(@PathVariable Long id) {
        boolean result = this.sysUserService.removeById(id);
        if (result) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }
    //更改用户状态
    @ApiOperation("更改用户状态")
    @GetMapping("/updateStatus/{id}/{status}")
    public  Result updateStatus(@PathVariable Long id,
                                @PathVariable Integer status){
        this.sysUserService.updateStatus(id,status);
        return Result.ok();
    }

}

